//
//  Scan_VC.m
//  仿支付宝
//
//  Created by 张国兵 on 15/12/9.
//  Copyright © 2015年 zhangguobing. All rights reserved.

#import "Scan_VC.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import "UIView+SDExtension.h"
#import "ZXingObjC.h"
#import "ZSQrCodeCoverView.h"
#import <netdb.h>
#import <SystemConfiguration/SCNetworkReachability.h>

static const CGFloat kBorderW = 100;
static const CGFloat kMargin = 30;
@interface Scan_VC ()<UIAlertViewDelegate,AVCaptureMetadataOutputObjectsDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate, ZSQrCodeCoverViewDelegate>{

    
}
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, weak)   UIView *maskView;
@property (nonatomic, strong) UIView *scanWindow;
@property (nonatomic, strong) UIImageView *scanNetImageView;
@property(nonatomic, weak) ZSQrCodeCoverView *qrCodeCoverView;

@property(atomic, assign) BOOL scanLockIsLock;

@property(nonatomic, assign) BOOL isParsingPhoto;

@end

@implementation Scan_VC
-(void)viewWillAppear:(BOOL)animated{
    
    
    [super viewWillAppear:animated];
    
    if (self.isParsingPhoto) {
        //正在解析照片
        self.isParsingPhoto = NO;
    } else {
        if (![_session isRunning]) {
            if ([self isNetworkReachable]) {
                //有网络才可以扫描
                self.scanLockIsLock = NO;

                [_session startRunning];
            }
        }
    }
    self.title = @"扫一扫";
    [self resumeAnimation];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //self.navigationController.navigationBar.hidden=NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //这个属性必须打开否则返回的时候会出现黑边
    self.view.clipsToBounds=YES;
    //1.遮罩
    [self setupMaskView];
    //5.扫描区域
    [self setupScanWindowView];
    //3.提示文本
    [self setupTipTitleView];
    //4.顶部导航
    [self setupNavView];
    
    //6.开始动画
    [self beginScanning];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(resumeAnimation) name:@"EnterForeground" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeAnimation) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    //请在设备的"设置-隐私-相机"中允许访问相机
    NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudio
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus ==AVAuthorizationStatusRestricted){
        
    }else if(authStatus == AVAuthorizationStatusDenied){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"请在设备的\"设置-隐私-相机\"中允许访问相机。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
    
        alert.tag = 1008;
        [alert show];
        return;
    }
    
    
}
-(void)setupTipTitleView{
    
    //1.补充遮罩,这是底部的灰色遮罩，iphone6p显示不正常就是因为这个遮罩不够长
    UIView*mask=[[UIView alloc]initWithFrame:CGRectMake(0, _maskView.sd_y+_maskView.sd_height, self.view.sd_width, kBorderW + 500)];
    mask.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    [self.view addSubview:mask];
    
    //2.操作提示 self.view.sd_height*0.9-kBorderW*2
    UILabel * tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_scanWindow.frame), self.view.bounds.size.width, kBorderW)];
    tipLabel.text = @"将二维码放入扫描框内，即可自动扫描";
    tipLabel.textColor = [UIColor whiteColor];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.lineBreakMode = NSLineBreakByWordWrapping;
    tipLabel.numberOfLines = 2;
    tipLabel.font=[UIFont systemFontOfSize:12];
    tipLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tipLabel];
    
}
-(void)setupNavView{
    
    //1.返回
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(20, 30, 40, 25);
    [backBtn setImage:[UIImage imageNamed:@"qrcode_scan_titlebar_back_nor"] forState:UIControlStateNormal];
    backBtn.contentMode=UIViewContentModeScaleAspectFit;
    [backBtn addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    //2.相册
    UIButton * albumBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    albumBtn.frame = CGRectMake(0, 0, 35, 49);
    albumBtn.center=CGPointMake(self.view.sd_width/2, 20+49/2.0);
    [albumBtn setBackgroundImage:[UIImage imageNamed:@"qrcode_scan_btn_photo_down"] forState:UIControlStateNormal];
    albumBtn.contentMode=UIViewContentModeScaleAspectFit;
    [albumBtn addTarget:self action:@selector(myAlbum) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:albumBtn];
    //3.闪光灯
    UIButton * flashBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    flashBtn.frame = CGRectMake(self.view.sd_width-55,20, 35, 49);
    [flashBtn setBackgroundImage:[UIImage imageNamed:@"qrcode_scan_btn_flash_down"] forState:UIControlStateNormal];
    flashBtn.contentMode=UIViewContentModeScaleAspectFit;
    [flashBtn addTarget:self action:@selector(openFlash:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:flashBtn];

}
- (void)setupMaskView
{
    UIView *mask = [[UIView alloc] init];
    _maskView = mask;
    
    mask.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7].CGColor;
    mask.layer.borderWidth = kBorderW;
    
    mask.bounds = CGRectMake(0, 0, self.view.sd_width + kBorderW + kMargin , self.view.sd_width + kBorderW + kMargin);
    mask.center = CGPointMake(self.view.sd_width * 0.5, self.view.sd_height * 0.5);
    mask.sd_y = 0;
    
    [self.view addSubview:mask];
}

- (void)setupBottomBar

{
    //1.下边栏
    UIView *bottomBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.sd_height * 0.9, self.view.sd_width, self.view.sd_height * 0.1)];
    bottomBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    
    [self.view addSubview:bottomBar];
    
    //2.我的二维码
    UIButton * myCodeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    myCodeBtn.frame = CGRectMake(0,0, self.view.sd_height * 0.1*35/49, self.view.sd_height * 0.1);
    myCodeBtn.center=CGPointMake(self.view.sd_width/2, self.view.sd_height * 0.1/2);
    [myCodeBtn setImage:[UIImage imageNamed:@"qrcode_scan_btn_myqrcode_down"] forState:UIControlStateNormal];
 
    myCodeBtn.contentMode=UIViewContentModeScaleAspectFit;
    
    [myCodeBtn addTarget:self action:@selector(myCode) forControlEvents:UIControlEventTouchUpInside];
    [bottomBar addSubview:myCodeBtn];
    
   
}
- (void)setupScanWindowView
{
    CGFloat scanWindowH = self.view.sd_width - kMargin * 2;
    CGFloat scanWindowW = self.view.sd_width - kMargin * 2;
    _scanWindow = [[UIView alloc] initWithFrame:CGRectMake(kMargin, kBorderW - 4, scanWindowW, scanWindowH)];
    _scanWindow.clipsToBounds = YES;
    [self.view addSubview:_scanWindow];

    _scanNetImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scan_net"]];
    CGFloat buttonWH = 18;
    
    UIButton *topLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWH, buttonWH)];
    [topLeft setImage:[UIImage imageNamed:@"scan_1"] forState:UIControlStateNormal];
    [_scanWindow addSubview:topLeft];
    
    UIButton *topRight = [[UIButton alloc] initWithFrame:CGRectMake(scanWindowW - buttonWH, 0, buttonWH, buttonWH)];
    [topRight setImage:[UIImage imageNamed:@"scan_2"] forState:UIControlStateNormal];
    [_scanWindow addSubview:topRight];
    
    UIButton *bottomLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, scanWindowH - buttonWH, buttonWH, buttonWH)];
    [bottomLeft setImage:[UIImage imageNamed:@"scan_3"] forState:UIControlStateNormal];
    [_scanWindow addSubview:bottomLeft];
    
    UIButton *bottomRight = [[UIButton alloc] initWithFrame:CGRectMake(topRight.sd_x, bottomLeft.sd_y, buttonWH, buttonWH)];
    [bottomRight setImage:[UIImage imageNamed:@"scan_4"] forState:UIControlStateNormal];
    [_scanWindow addSubview:bottomRight];
}

- (void)beginScanning
{
    //获取摄像设备
    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //创建输入流
    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    if (!input) return;
    //创建输出流
    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
    //设置代理 在主线程里刷新
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //设置有效扫描区域
    CGRect scanCrop=[self getScanCrop:_scanWindow.bounds readerViewBounds:self.view.frame];
     output.rectOfInterest = scanCrop;
    //初始化链接对象
    _session = [[AVCaptureSession alloc]init];
    //高质量采集率
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    
    [_session addInput:input];
    [_session addOutput:output];
    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)
//    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
    
    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode];
    
    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
    CGRect bounds = self.view.layer.bounds;
    layer.frame= CGRectMake(0, 0, bounds.size.width, bounds.size.height);
//    layer.frame = [UIScreen mainScreen].bounds;
    
    [self.view.layer insertSublayer:layer atIndex:0];
    
    ///在这里判断，如果网络不可用就不扫描
    if (![self isNetworkReachable]) {
        [UIView animateWithDuration:0.25 animations:^{
            self.qrCodeCoverView.alpha = 1;
        }];
    } else {
        //开始捕获
        self.scanLockIsLock = NO;
        [_session startRunning];

    }

    
}

#pragma mark - 扫描结果
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects.count>0) {
        [_session stopRunning];
        //同一时间还没暂停可能就已经有多次结果，会导致处理多次
        if (!self.scanLockIsLock) {
            self.scanLockIsLock = YES;
            AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
            [self dealWithMetadataObjectsStringValue:metadataObject.stringValue];
        }
    }
}

#pragma mark-> 扫描完毕结果处理分支
- (void)dealWithMetadataObjectsStringValue:(NSString *)metadataObjectsStringValue {
    if ([metadataObjectsStringValue rangeOfString:@"gzda"].location != NSNotFound) {
        
        
        
    } else {
        if ([metadataObjectsStringValue rangeOfString:@"http"].location != NSNotFound) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"可能存在风险，是否打开此链接？" message:metadataObjectsStringValue delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"打开连接", nil];
            alert.tag = 1004;
            alert.message = metadataObjectsStringValue;
            [alert show];
        } else {
            //其他内容
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"扫描结果" message:metadataObjectsStringValue delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"复制", nil];
            alert.message = metadataObjectsStringValue;
            alert.tag = 1005;
            [alert show];
        }
        
    }
}




#pragma mark - UIAlertViewDelegate 扫面结果弹窗
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1001) {
        //目前尚未用到，预留的
    } else if (alertView.tag == 1002) {
        //目前尚未用到，预留的
    } else if (alertView.tag == 1003) {
        //目前尚未用到，预留的
    } else if (alertView.tag == 1004) {
        //非官方网站
        if (buttonIndex == 0) {
            self.scanLockIsLock = NO;//先打开线程锁开关
            [_session startRunning];
        } else if (buttonIndex == 1) {
            //打开连接
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:alertView.message]];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    
    } else if (alertView.tag == 1005){
        //其他字符串
        if (buttonIndex == 0) {
            self.scanLockIsLock = NO;//先打开线程锁开关
            [_session startRunning];
        } else if (buttonIndex == 1){
            //复制内容
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = alertView.message;
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    } else if(alertView.tag == 1006) {
        //相册不允许访问
        if (![_session isRunning]) {
            self.scanLockIsLock = NO;
            [_session startRunning];
        }
    } else if (alertView.tag == 1007) {
        //相册解析失败
        if (![_session isRunning]) {
            self.scanLockIsLock = NO;
            [_session startRunning];
        }
    } else if (alertView.tag == 1008) {
        //相机被拒绝访问
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}




#pragma mark-> 我的相册
-(void)myAlbum{
    if ([_session isRunning]) {
        //要马上停止
        self.scanLockIsLock = YES;
        [_session stopRunning];
    }
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        //1.初始化相册拾取器
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.navigationBar.tintColor = [UIColor blackColor];
        //2.设置代理
        controller.delegate = self;
        //3.设置资源：
        /**
         UIImagePickerControllerSourceTypePhotoLibrary,相册
         UIImagePickerControllerSourceTypeCamera,相机
         UIImagePickerControllerSourceTypeSavedPhotosAlbum,照片库
         */
        controller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        //4.随便给他一个转场动画
        controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:controller animated:YES completion:nil];
        
    }else{
        
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"设备不支持访问相册，请在设置->隐私->照片中进行设置！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 1006;
        [alert show];
    }
    
}

#pragma mark - 照片处理
-(void)getInfoWithImage:(UIImage *)img{
    
    UIImage *loadImage= img;
    CGImageRef imageToDecode = loadImage.CGImage;
    
    ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode];
    ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];
    
    NSError *error = nil;
    
    ZXDecodeHints *hints = [ZXDecodeHints hints];
    
    ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
    ZXResult *result = [reader decode:bitmap
                                hints:hints
                                error:&error];
    
    if (result) {
        NSString *contents = result.text;
        
        [self dealWithMetadataObjectsStringValue:contents];
        
    } else {
        [self showInfoWithMessage:nil andTitle:@"图片中没有有效的二维码信息"];
    }
}


#pragma mark - 扫描相册里的图片完成后在这里进行操作
- (void)showInfoWithMessage:(NSString *)message andTitle:(NSString *)title
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alter.tag = 1007;
    [alter show];
    
}





#pragma mark-> 相册选择图片回调imagePickerController delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //1.获取选择的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    //选中图片就开始解析了
    self.isParsingPhoto = YES;
    //系统原生在初始化监控器的时候在ios7或iphone4崩溃了，因此改用ZXingObjc
//    CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyLow }];
    [self dismissViewControllerAnimated:YES completion:^{
            [self getInfoWithImage:image];
        }];
 
}
#pragma mark-> 闪光灯
-(void)openFlash:(UIButton*)button{
    button.selected = !button.selected;
    if (button.selected) {
        [self turnTorchOn:YES];
    }
    else{
        [self turnTorchOn:NO];
    }
    
}
#pragma mark-> 我的二维码
-(void)myCode{
    

    
    
}
#pragma mark-> 开关闪光灯
- (void)turnTorchOn:(BOOL)on
{
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        if ([device hasTorch] && [device hasFlash]){
            
            [device lockForConfiguration:nil];
            if (on) {
                [device setTorchMode:AVCaptureTorchModeOn];
                [device setFlashMode:AVCaptureFlashModeOn];
                
            } else {
                [device setTorchMode:AVCaptureTorchModeOff];
                [device setFlashMode:AVCaptureFlashModeOff];
            }
            [device unlockForConfiguration];
        }
    }
}


#pragma mark 恢复动画
- (void)resumeAnimation
{
    CAAnimation *anim = [_scanNetImageView.layer animationForKey:@"translationAnimation"];
    if(anim){
//        // 1. 将动画的时间偏移量作为暂停时的时间点
//        CFTimeInterval pauseTime = _scanNetImageView.layer.timeOffset;
//        // 2. 根据媒体时间计算出准确的启动动画时间，对之前暂停动画的时间进行修正
//        CFTimeInterval beginTime = CACurrentMediaTime() - pauseTime;
//        // 3. 要把偏移时间清零
//        [_scanNetImageView.layer setTimeOffset:0.0];
//        // 4. 设置图层的开始动画时间
//        [_scanNetImageView.layer setBeginTime:beginTime];
//        
//        [_scanNetImageView.layer setSpeed:1.0];
        
    }else{
        
        CGFloat scanNetImageViewH = 241;
        CGFloat scanWindowH = self.view.sd_width - kMargin * 2;
        CGFloat scanNetImageViewW = _scanWindow.sd_width;
    
        _scanNetImageView.frame = CGRectMake(0, -scanNetImageViewH, scanNetImageViewW, scanNetImageViewH);
        CABasicAnimation *scanNetAnimation = [CABasicAnimation animation];
        scanNetAnimation.keyPath = @"transform.translation.y";
        scanNetAnimation.byValue = @(scanWindowH);
        scanNetAnimation.duration = 1.0;
        scanNetAnimation.repeatCount = MAXFLOAT;
        [_scanNetImageView.layer addAnimation:scanNetAnimation forKey:@"translationAnimation"];
        [_scanWindow addSubview:_scanNetImageView];
    }
}

#pragma mark - 移除动画




#pragma mark-> 获取扫描区域的比例关系
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{
    
    CGFloat x,y,width,height;
    
    x = (CGRectGetHeight(readerViewBounds)-CGRectGetHeight(rect))/2/CGRectGetHeight(readerViewBounds);
    y = (CGRectGetWidth(readerViewBounds)-CGRectGetWidth(rect))/2/CGRectGetWidth(readerViewBounds);
    width = CGRectGetHeight(rect)/CGRectGetHeight(readerViewBounds);
    height = CGRectGetWidth(rect)/CGRectGetWidth(readerViewBounds);
    
    return CGRectMake(x, y, width, height);
    
}
#pragma mark-> 返回
- (void)disMiss
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 判断是否有网络
-(BOOL)isNetworkReachable{
    // Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    
    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    
    if (!didRetrieveFlags)
    {
        return NO;
    }
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    return (isReachable && !needsConnection) ? YES : NO;
}


#pragma mark-> ZSQrCodeCoverView代理方法
- (void)qrCodeCoverViewCoverBtnDidClick:(ZSQrCodeCoverView *)qrCodeView {
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - 懒加载
- (ZSQrCodeCoverView *)qrCodeCoverView {
    if (_qrCodeCoverView == nil) {
        ZSQrCodeCoverView *view = [ZSQrCodeCoverView qrCodeCoverView];
        [self.view addSubview:view];
        _qrCodeCoverView = view;
        view.delegate = self;
        _qrCodeCoverView.frame = self.view.frame;
        _qrCodeCoverView.alpha = 0;
    }
    return _qrCodeCoverView;
}



- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"EnterForeground" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
    
}






@end
